package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.TrainEnterBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.TrainService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class TrainEnterActivity extends AppCompatActivity {
    private int user_id;
    private String trainname;
    private int train_id;
    private String trainmoney;
    private String trainnotice;

    private TextView train_name;
    private TextView train_money;
    private TextView train_notice;

    private EditText train_username;
    private EditText train_tel;
    private ImageButton jian;
    private TextView number;
    private String payway;
    private int trainorder_id;

    private ImageButton jia;
    private RadioGroup radioGroup;
    private RadioButton weixin;
    private RadioButton zhifubao;
    private TextView train_count;
    private Button count_button;
    private ImageButton back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_enter);


        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
         user_id = sharedPreferences.getInt("user_id", 0);
         train_id = getIntent().getIntExtra("train_id", 0);

        trainname = getIntent().getStringExtra("train_name");
        train_name = (TextView) findViewById(R.id.train_name);
        train_name.setText(trainname);

        trainmoney = getIntent().getStringExtra("train_money");
        train_money = (TextView) findViewById(R.id.train_money);
        train_money.setText(trainmoney);


        trainnotice = getIntent().getStringExtra("train_notice");
        train_notice = (TextView) findViewById(R.id.train_notice);
        train_notice.setText(trainnotice);

        train_username = (EditText) findViewById(R.id.train_username);
        train_tel = (EditText) findViewById(R.id.train_tel);
        jian = (ImageButton) findViewById(R.id.jian);
        number = (TextView) findViewById(R.id.number);
        jia = (ImageButton) findViewById(R.id.jia);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        weixin = (RadioButton) findViewById(R.id.weixin);
        zhifubao = (RadioButton) findViewById(R.id.zhifubao);
        train_count = (TextView) findViewById(R.id.train_count);
        count_button = (Button) findViewById(R.id.count_button);
        back = (ImageButton) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                finish();
                intent.setClass(TrainEnterActivity.this, TrainDetailActivity.class);
                startActivity(intent);
            }
        });

        jiajian();
        zhifu();
        count();


    }

    public void jiajian(){
        jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String shuliang = number.getText().toString();
                int n = Integer.parseInt(shuliang);
                number.setText(Integer.valueOf(shuliang) + 1 +"");
                int money_one = Integer.parseInt(trainmoney);
                int zongjia = (n+1) * money_one;
                String t = String.valueOf(zongjia);
                train_count.setText(t + "元");

            }
        });
        jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String shuliang = number.getText().toString();
                int n = Integer.parseInt(shuliang);
               if (n>0){
                   number.setText(Integer.valueOf(shuliang) - 1 +"");
                   int money_one = Integer.parseInt(trainmoney);
                   int zongjia = (n-1) * money_one;
                   String t = String.valueOf(zongjia);
                   train_count.setText(t + "元");
               }
               else{
                   Toast.makeText(TrainEnterActivity.this, "不能少于1", Toast.LENGTH_LONG).show();

               }


            }
        });



    }


    public void zhifu(){
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if (weixin.getId() == checkedId){
                    payway = weixin.getText().toString();
                }
                if (zhifubao.getId() == checkedId){
                    payway = zhifubao.getText().toString();
                }
                Toast.makeText(getApplicationContext(),"当前选择的付款方式为:"+payway,Toast.LENGTH_LONG).show();
            }
        });
        radioGroup.check(R.id.weixin);
    }
    public void count(){
        count_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Const.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                TrainService service = retrofit.create(TrainService.class);
                String user_name = train_username.getText().toString();
                String user_tel = train_tel.getText().toString();

                String counts = number.getText().toString();
                int count = Integer.parseInt(counts);
                Call<TrainEnterBean> call = service.
                        trainorderadd(user_id,train_id,count,payway,user_name,user_tel);
                call.enqueue(new Callback<TrainEnterBean>() {
                    @Override
                    public void onResponse(Call<TrainEnterBean> call, Response<TrainEnterBean> response) {
                        trainorder_id = response.body().getTrainorder_id();
                        Intent intent = new Intent(TrainEnterActivity.this,TrainOrderActivity.class);
                        intent.putExtra("trainorder_id",trainorder_id);
                        Toast.makeText(TrainEnterActivity.this, "购买成功", Toast.LENGTH_LONG).show();
                        startActivity(intent);
                    }

                    @Override
                    public void onFailure(Call<TrainEnterBean> call, Throwable t) {

                    }


                });
            }
        });

    }

}
